[PHP] Difference between normal and magic setters and getters

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-04-05T19:31:35Z Indexed on 2010/04/05 19:33 UTC
Read the original article Hit count: 425

I am using a magic getter/setter class for my session variables, but I don't see any difference between normal setters and getters.

The code:

class session
{
    public function __set($name, $value)
    {
        $_SESSION[$name] = $value;
    }

    public function __unset($name)
    {
        unset($_SESSION[$name]);
    }

    public function __get($name)
    {
        if(isset($_SESSION[$name]))
        {
            return $_SESSION[$name];
        }
    }
}

Now the first thing I noticed is that I have to call $session->_unset('var_name') to remove the variable, nothing 'magical' about that.

Secondly when I try to use $session->some_var this does not work. I can only get the session variable using $_SESSION['some_var'].

I have looked at the PHP manual but the functions look the same as mine.

Am I doing something wrong, or is there not really anything magic about these functions.

© Stack Overflow or respective owner

Related posts about php

Related posts about magic-function